﻿@typeparam TModel where TModel : GlobalDefs

<MExpansionPanels Flat>
    <MExpansionPanel>
        <MExpansionPanelHeader Color="primary" HideActions>
            <span class="text-h7">全局配置</span>
        </MExpansionPanelHeader>
        <MExpansionPanelContent>
            <MRow Class="mt-4">
                <MCol Cols="12" Md="6">
                    <MTextField @bind-Value="Model.RouterId"
                                Label="路由器标识"
                                Outlined
                                Dense
                                Required
                                Rules="_requiredRules" />
                </MCol>
                <MCol Cols="12" Md="6">
                    <MSelect @bind-Value="Model.Interface"
                             ItemText="item => item"
                             ItemValue="item => item"
                             Items="_networkInterfaces"
                             Label="默认网络接口"
                             Outlined
                             Dense />
                </MCol>
            </MRow>

            <MRow>
                <MCol Cols="12">
                    <MCard Flat>
                        <MCardTitle>通知设置</MCardTitle>
                        <MCardText>
                            <MList>
                                @for (int i = 0; i<Model.NotificationEmails.Count;i++)
                                {
                                    var email=Model.NotificationEmails[i];
                                    <MListItem>
                                        <MListItemContent>
                                            <MTextField @bind-Value="email"
                                                        Label="通知邮箱"
                                                        Outlined
                                                        Dense
                                                        Rules="_emailRules" />
                                        </MListItemContent>
                                        <MListItemAction>
                                            <MButton Icon
                                                     Color="error"
                                                     OnClick="@(() => RemoveEmail(email))">
                                                <MIcon>mdi-delete</MIcon>
                                            </MButton>
                                        </MListItemAction>
                                    </MListItem>
                                }
                                <MListItem>
                                    <MButton Color="primary"
                                             Outlined
                                             OnClick="AddEmail">
                                        <MIcon Left>mdi-plus</MIcon>
                                        添加通知邮箱
                                    </MButton>
                                </MListItem>
                            </MList>
                        </MCardText>
                    </MCard>
                </MCol>
            </MRow>

            <MRow>
                <MCol Cols="12" Md="6">
                    <MTextField @bind-Value="Model.SmtpServer"
                                Label="SMTP服务器"
                                Outlined
                                Dense />
                </MCol>
                <MCol Cols="12" Md="6">
                    <MTextField @bind-Value="Model.SmtpConnectTimeout"
                                Label="SMTP超时(秒)"
                                Type="number"
                                Outlined
                                Dense />
                </MCol>
            </MRow>

            <MRow>
                <MCol Cols="12">
                    <MCard Flat>
                        <MCardTitle>高级选项</MCardTitle>
                        <MCardText>
                            <MSwitch @bind-Value="Model.VrrpStrict"
                                     Label="启用严格模式"
                                     MessagesGetter="context => context ? '已启用' : '已禁用'"
                                     Dense />
                            
                            <MSwitch @bind-Value="Model.VrrpSkipCheckAdvAddr"
                                     Label="跳过检查通告地址"
                                     MessagesGetter="context => context ? '已启用' : '已禁用'"
                                     Dense />
                        </MCardText>
                    </MCard>
                </MCol>
            </MRow>
        </MExpansionPanelContent>
    </MExpansionPanel>
</MExpansionPanels>

@code {
    [Parameter]
    public TModel Model { get; set; }

    [Parameter]
    public EventCallback<TModel> ModelChanged { get; set; }

    private List<string> _networkInterfaces = new() { "eth0", "eth1", "bond0" };
    private List<Func<string, StringBoolean>> _requiredRules = new()
    {
        value => string.IsNullOrWhiteSpace(value) ? "必填字段" : true
    };
    private List<Func<string, StringBoolean>> _emailRules = new()
    {
        value => string.IsNullOrWhiteSpace(value) ? "必填字段" : true,
        value => !value.Contains('@') ? "请输入有效的邮箱地址" : true
    };

    private void AddEmail()
    {
        Model.NotificationEmails.Add("");
    }

    private void RemoveEmail(string email)
    {
        Model.NotificationEmails.Remove(email);
    }
}